package com.treasure.service.task.service;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 
 * @version 1.0 
 * @author panxinwen 2018/10/29 16:19
 * @Copyright © 2015 | 武汉京航科技有限公司 Email:19919866605@163.com
 * */

public class Response<T> implements Serializable {

    private int code;
    private String message;
    private String msg;
    private T data;
    private String token;

    public Response(){

    }
    public Response(int code) {
        this.code = code;
    }
    public Response(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public Response(int code, String message) {
        this.code = code;
        this.message = message;
        this.msg = message;
    }

    public Response(int code, T data, String token) {
        this.code = code;
        this.data = data;
        this.token = token;
    }


    public static Response success(Object data) {
        return new Response(200, data);
    }
    public static Response success(Object data,String token) {
        return new Response(200, data,token);
    }

    public static Response success() {
        return new Response(200,"成功");
    }

    public static Builder newSuccessBuilder(){
        return new Builder(200);
    }
    public static Response failure(int code, String message) {
        return new Response(code, message);
    }

    public static Response failure(String message) {
        return new Response(1, message);
    }

    public static Response failure() {
        return new Response(201, "");
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public <T> T getParseData(Class<T> clazz) {
        try {
            if(getData() !=null )
                return JSON.parseObject(JSON.toJSONString(getData()),clazz);
        }catch (Exception e ){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * <p>redirect.</p>
     *
     * @param path a {@link String} object.
     */
    public static Response redirect(String path) {
        return new Builder(201).put("url", path).build();
    }


    public static class Builder {
        private Response<Object> response;

        private int code;
        private Map<String, Object> mapData;

        public Builder(int errorCode) {
            response = new Response<>();

            this.code = errorCode;
            this.mapData = new HashMap<>();
        }

        public Builder put(String key, Object value) {
            mapData.put(key, value);
            return this;
        }

        public Response build() {
            response.setCode(this.code);
            response.setData(this.mapData);
            return response;
        }
    }
    /**
     * <p>Getter for the field <code>data</code>.</p>
     *
     * @return a T object.
     */
    public T getData() {
        return data;
    }

    /**
     * <p>Setter for the field <code>data</code>.</p>
     *
     * @param data a T object.
     */
    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
